#!/bin/bash -e

prog=`basename $0`
args=$@

function info() {
cat /mnt/ilustre/app/medical/tools/script/info.snp.txt
exit 65
}

while getopts  ":a:s:i:l:d:gkLGnc:Ap:m:q:r:t:v" opt
do
        case  $opt  in
		a) cutx=$OPTARG;;
        i)
			interval=$OPTARG;;
		l)
			target=$OPTARG;;
		g) single_cell=T;;
		G) somatic=T;;
		d) desti=$OPTARG;;
		k) mark=T;;
		n)
			cutadaptor=$OPTARG;;
		c)
			cnv=$OPTARG;;
		r) realn_interval=$OPTARG;;
		m)
			mem=$OPTARG;;
		p)
			cpu=$OPTARG;;
		q)
			query=$OPTARG;;
		s) sample_name=$OPTARG;;
		L) run_local=T;;
		t) run_continue_dir=$OPTARG;;
		v) cov=T;;
		h) info;;
        esac
done
shift $(($OPTIND - 1))

if [ -z "$2" ]; then info; fi


# if [ -z "$query" ]; then query=$(id -gn); fi
# if test "$query" = "medical"; then query=md; fi



if test -z "$run_local"; then
sub -n$sample_name -p${cpu:=8} -m${mem:=20} -q${query:=md} $prog -L $args || exit 0
	# if test -z "$cpu"; then
		# if test -z "$mem"; then
			# sub -n$sample_name -q$query $prog -L $args
		# else
			# sub -n$sample_name -m$mem -q$query $prog -L $args
		# fi
	# else
		# if test -z "$mem"; then
			# sub -n$sample_name -p$cpu -q$query $prog -L $args
		# else
			# sub -n$sample_name -m$mem -q$query -p$cpu $prog -L $args
		# fi
	# fi

clear
echo A snp calling and annotion job, named $sample_name, was submitted to pbs...
exit 0
fi

echo $prog $args >>.log || exit 0

. $var

done=done.sh
mnt=mnt.sh
makedir=makedir.sh
path_abs=path_abs.sh
fastqstat=fastqstat.sh
fastqc=fastqc.sh
aln=aln_bwa_bt.sh
sam2b=sam2b_samtools.sh
deldup=deldup.sh
realign_bt=realign_bt.sh
summ=summ.sh

snapshot=$out_prefix.snapshot.txt

cut_suf1=1.cut.fastq
cut_suf2=2.cut.fastq
aln_suf=sam
sam2b_suf=sort.bam
deldup_suf=deldup.bam


r1=`$path_abs $1`
r2=`$path_abs $2`
r12=`echo $r1 $r2`

if test -z "$run_continue_dir"; then
run_continue_dir=`$makedir -p$sample_name` && cd $run_continue_dir
else 
cd $run_continue_dir
fi


if [ -z "$cutx" ]; then cutx=malbac; fi
if [ -z "$desti" ]; then desti=snp; fi

if test "$(id -u)" != "577"; then
desti=$desti/$(id -un)
fi


if test "$cutx" = "malbac"; then
	cutadp=cutadp_malbac.sh
elif test "$cutx" = "general"; then
	cutadp=cutadp.sh
fi


if [ "$interval" = "brca12" ]; then
	cutadp=cutadp.sh
	target=$data_path/intervals/1/brca.qiagen.NGHS-102X-Covered.b37.bed
elif [ "$interval" = "brca44" ]; then
	cutadp=cutadp.sh
	target=$data_path/intervals/1/brca.qiagen.NGHS-001X-Covered.b37.bed
elif [ "$interval" = "ctc80" ]; then
	cutadp=cutadp_ctc80_discard_trimmed.sh
	target=$data_path/intervals/1/ctc80.bed
elif [ "$interval" = "brca12mor" ]; then
	cutadp=cutadp_ph_miseq.sh
	target=$data_path/intervals/1/brca12_mor.bed
elif [ "$interval" = "ca50mor" ]; then
	cutadp=cutadp_ph_miseq.sh
	target=$data_path/intervals/1/ca50_mor.bed
elif [ "$interval" = "exomes" ]; then
	target=$data_path/intervals/1/b37.agilent_Exon_V5_UTRs.bed
elif [ "$interval" = "wg" ]; then
	target=$data_path/intervals/1/b37.bed
else
	cutadp=cutadp.sh
fi


echo; echo reads: $r12
echo target: $target
echo cutadp: $cutadp
echo desti: $desti
echo sample name: $sample_name
echo target: $target
echo interval: $interval


test -e $snapshot || echo -e "Sample\t$sample_name\nRaw data\t$r12\nPanel\t$interval\nResults\t${http}/$desti/" >$snapshot


if test ${cpu_num:=8} -gt 1; then
ts0=`echo ${cpu_num}-3|bc`
else
ts0=1
fi


$done $fastqstat || $fastqstat -s$sample_name -t2 -p$out_prefix $r12 &


$done $fastqc || $fastqc -s$sample_name $r12 &


if test "$cutx" = "no"; then
cp $r1 $out_prefix.$cut_suf1
cp $r2 $out_prefix.$cut_suf2
fi


$done $cutadp || $cutadp -p$out_prefix $out_prefix.$cut_suf1 $out_prefix.$cut_suf2



$done $aln || $aln -t$ts0 -s$sample_name $out_prefix.$cut_suf1 $out_prefix.$cut_suf2 

# rm $out_prefix.$cut_suf1 $out_prefix.$cut_suf2


$done $sam2b || $sam2b -t$ts0 $out_prefix.$aln_suf




test -n "$cov" && $summ -s$sample_name -g$out_prefix.$sam2b_suf $target && . $cmd_done

:<<testt
# markdup
if test "$mark" = "T"; then

	if
	cat $pid_log|grep "$deldup $out_prefix.sort.bam done" >/dev/null; then
	echo $deldup done
	else
		# delete symbolic link if exists.
		if test -e $out_prefix.markdup.bam; then
			rm $out_prefix.markdup.bam*;
		fi
		
		$deldup -m $out_prefix.sort.bam
	fi

else
	ln -fs $out_prefix.sort.bam $out_prefix.markdup.bam
	if test -e $out_prefix.sort.bam.bai; then
		ln -fs $out_prefix.sort.bam.bai $out_prefix.markdup.bam.bai
	elif test -e $out_prefix.sort.bai; then
		ln -fs $out_prefix.sort.bai $out_prefix.markdup.bai
	fi
	
fi
# end markdup 

# cnv
if test -n "$cnv"; then

	if test "$cnv" = "hmmcp"; then
	if
	cat $pid_log|grep "$deldup $out_prefix.markdup.bam done" >/dev/null; then
	echo $deldup done
	else
	$deldup $out_prefix.$sam2b_suf
	fi

	hmmcopy0917b38.sh -w50000 -b37 $out_prefix.$deldup_suf
	hmmcopy0917b38.sh -w200000 -b37 $out_prefix.$deldup_suf
	hmmcopy0917b38.sh -w1000000 -b37 $out_prefix.$deldup_suf
	fi
fi



if

cat $pid_log|grep "$realn $out_prefix.markdup.bam done" >/dev/null; then
echo $realign_bt done
else


if test -n "$realn_interval"; then

if test -n "$target"; then
echo realign with known interval $realn_interval in target $target
realign_bt.sh -r$realn_interval -i$target $out_prefix.markdup.bam
else
realign_bt.sh -r$realn_interval $out_prefix.markdup.bam
fi


elif test -n "$target"; then
echo realign in $target
realign_bt.sh -i$target $out_prefix.markdup.bam
else
echo realign in whole region
realign_bt.sh $out_prefix.markdup.bam
fi

fi

recal=recal.sh
if
cat $pid_log|grep "$recal done" >/dev/null; then
echo $recal done
else

recal.sh -p1 -i$target
fi

if
cat $pid_log|grep "$fastqc $out_prefix.realn.recal.bam done" >/dev/null; then
echo $fastqc done
else
$fastqc -s$sample_name $out_prefix.realn.recal.bam &
fi

# calling 
cal0=ug.sh
if
cat $pid_log|grep "$cal0 $out_prefix.realn.recal.bam done" >/dev/null; then
echo $cal0 $out_prefix.realn.recal.bam done
else
	if test -n "$somatic"; then
		echo somatic or low coverage
        if test -n "$single_cell"; then
            echo single cell
            $cal0 -G1 -p$out_prefix -i$target $out_prefix.realn.recal.bam
        else
            $cal0 -G -p$out_prefix -i$target $out_prefix.realn.recal.bam
		fi
	else
        if test -n "$single_cell"; then
            $cal0 -1 -p$out_prefix -i$target $out_prefix.realn.recal.bam
        else
            $cal0 -p$out_prefix -i$target $out_prefix.realn.recal.bam
        fi
	fi
fi

summ=summ.sh
if
cat $pid_log|grep "$summ $out_prefix.realn.recal.bam $target done" >/dev/null; then
echo $summ done
else
$summ -g -s$sample_name $out_prefix.realn.recal.bam $target #can not background

ln $out_prefix.gene_summ.txt gene_summ.txt
fi


anno=anno_110416.sh
if
cat $pid_log|grep "$anno $out_prefix.snp.vcf $out_prefix.indel.vcf done" >/dev/null; then
echo $anno done
else

if test -n "$somatic"; then
	echo somatic
	if test -n "$single_cell"; then
		echo;echo;echo single_cell
		$anno -m -g -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	else
		$anno -m -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	fi
else
	echo germline
	if test -n "$single_cell"; then
		echo;echo;echo single_cell
		$anno -g -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	else
		$anno -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	fi
fi

fi

format_simple=format_top_simple.sh
if
cat $pid_log|grep "$format_simple done" >/dev/null; then
echo $format_simple done
else
format_top_simple.sh &
fi

format=format_top_0222.sh
if

cat $pid_log|grep "$format $out_prefix.high.vcf $out_prefix.low.vcf done" >/dev/null; then
echo $format done
else
# format_top_0508.sh
format_top_0222.sh $out_prefix.high.vcf $out_prefix.low.vcf
fi


wait


rename=file_rename_0222.sh
if
# cat $pid_log|grep "^$rename done" >/dev/null; then
cat $pid_log|grep "$rename $sample_name done" >/dev/null; then
echo $rename done
else
file_rename_0222.sh $sample_name
fi



echo;echo; echo desti: $desti

bk.sh -t$desti ${sample_name}*simple_info*.xls ${sample_name}*summary*.xls ${sample_name}*stat*.xls ${sample_name}*reads*.xls ${sample_name}*.pdf ${sample_name}*.html .log* ${sample_name}*deleterious.txt ${sample_name}*drugs-prediction*


# bk_zip.sh ${sample_name}*simple_info*.xls ${sample_name}*summary*.xls ${sample_name}*stat*.xls ${sample_name}*reads*.xls ${sample_name}*.pdf ${sample_name}*2*.html ${sample_name}*drugs-prediction.html


. $cmd_done

testt